package com.rede.didiok.user.modulelist.pay.restapi;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import com.rede.didiok.common.enums.PayMethodEnum;
import com.rede.didiok.user.modulelist.pay.factory.PayServiceFactory;
import com.rede.didiok.user.modulelist.pay.manager.TradeManager;
import com.rede.didiok.user.modulelist.pay.model.vo.BuyVO;
import com.rede.didiok.user.modulelist.pay.model.vo.SponsorVO;

/**
 * 交易模块
 *
 * @author 遇见
 */
@RestController
@RequestMapping("/trade")
@Api(value = "交易模块", tags = {"交易模块"})
@Slf4j
public class TradeRestApi {

    @Resource
    TradeManager tradeManager;

    @ApiOperation(value = "阿里支付回调")
    @RequestMapping(value = "/alipay/callback")
    public String aliPayCallback() {
        log.info("[aliPayCallback] 阿里云支付回调");
        return PayServiceFactory.getPayService(PayMethodEnum.ALI_PAY).callback();
    }

    @ApiOperation(value = "微信支付回调")
    @RequestMapping(value = "/wxPay/callback")
    public String wechatPayCallback() {
        log.info("[aliPayCallback] 微信支付回调");
        return PayServiceFactory.getPayService(PayMethodEnum.WECHAT_PAY).callback();
    }

    @ApiOperation(value = "第三方微信支付回调")
    @RequestMapping(value = "/yunGouOSWxPay/callback")
    public String yunGouOSWechatPayCallback() {
        log.info("[aliPayCallback] 第三方微信支付回调");
        return PayServiceFactory.getPayService(PayMethodEnum.YUN_GOU_OS_WECHAT_PAY).callback();
    }

    @ApiOperation(value = "蓝兔支付第三方微信支付回调")
    @RequestMapping(value = "/lantuWxPay/callback")
    public String lantuWechatPayCallback(@RequestParam Map<String, String> data, HttpServletRequest request, HttpServletResponse response) {
        log.info("[lantuWechatPayCallback] 蓝兔支付第三方微信支付回调");

//        log.info("[lantuWechatPayCallback] 蓝兔支付第三方微信支付回调,data:{}", JSONObject.toJSONString(data));
//        HttpResponse<InputStream> response = request.asObject(raw -> raw.getContent());
//        InputStream responseInputStream = response.getBody();
//        log.info("蓝兔支付第三方微信支付回调 queryString:{}", );
//        log.info("蓝兔支付第三方微信支付回调 getHeaderNames:{}", JSONObject.toJSONString(request.getHeaderNames()));
////        log.info("蓝兔支付第三方微信支付回调 getHttpServletMapping:{}", JSONObject.toJSONString(request.getHttpServletMapping()));
//        log.info("蓝兔支付第三方微信支付回调 getMethod:{}", request.getMethod());
//        log.info("蓝兔支付第三方微信支付回调 getPathInfo:{}", request.getPathInfo());
//        log.info("蓝兔支付第三方微信支付回调 getRequestURL:{}", request.getRequestURL().toString());
//        log.info("蓝兔支付第三方微信支付回调 getServletPath:{}", request.getServletPath());
//        try {
////            log.info("蓝兔支付第三方微信支付回调 getParts:{}", JSONObject.toJSONString(request.getParts()));
//        } catch (IOException e) {
//            e.printStackTrace();
//        } catch (ServletException e) {
//            e.printStackTrace();
//        }
//        try {
//            String json = new String(readInputStream(responseInputStream), "UTF-8");
//            log.info("蓝兔支付第三方微信支付回调 json:{}", json);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        return PayServiceFactory.getPayService(PayMethodEnum.LANTU_WECHAT_PAY).callback();
    }

//    public static byte[] readInputStream(InputStream inStream) throws Exception {
//        ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
//        byte[] buffer = new byte[1024];
//        boolean var3 = false;
//
//        int len;
//        while((len = inStream.read(buffer)) != -1) {
//            outSteam.write(buffer, 0, len);
//        }
//
//        outSteam.close();
//        inStream.close();
//        return outSteam.toByteArray();
//    }

    @ApiOperation(value = "蓝兔支付第三方退款回调")
    @RequestMapping(value = "/lantuWxPay/callback/refund")
    public String lantuWechatPayRefundCallback() {
        log.info("[lantuWechatPayRefundCallback] 蓝兔支付第三方退款回调");
        return PayServiceFactory.getPayService(PayMethodEnum.LANTU_WECHAT_PAY).refundCallback();
    }

    /**
     * 立即购买
     *
     * @return
     */
    @ApiOperation(value = "立即购买")
    @PostMapping(value = "/buyNow")
    public String buyNow(@RequestBody BuyVO buyVO) {
        return tradeManager.buyNow(buyVO);
    }

    /**
     * 资金赞助
     *
     * @return
     */
    @ApiOperation(value = "资金赞助")
    @PostMapping(value = "/sponsor")
    public String sponsor(@RequestBody SponsorVO sponsorVO) {
        return tradeManager.sponsor(sponsorVO);
    }
}
